home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / minix / libsrc~1.z / libsrc~1 / putenv.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-12-28  |  2.4 KB  |  98 lines

  1. /****************************************************************/
  2. /*                                */
  3. /*    putenv(3)                        */
  4. /*                                */
  5. /*        Change or add an environment entry        */
  6. /*                                */
  7. /****************************************************************/
  8. /*   origination        1987-Oct-7               T. Holm    */
  9. /****************************************************************/
  10.  
  11. #include "lib.h"
  12. #include <stdio.h>
  13.  
  14.  
  15. #define  PSIZE  sizeof(char *)
  16.  
  17.  
  18. extern  char  **environ;
  19.  
  20. #ifndef __STDC__
  21. char  *index();
  22. char  *malloc();
  23. #endif
  24.  
  25. /****************************************************************/
  26. /*                                */
  27. /*    putenv( entry )                        */
  28. /*                                */
  29. /*        The "entry" should follow the form         */
  30. /*        "NAME=VALUE". This routine will search the     */
  31. /*        user environment for "NAME" and replace its     */
  32. /*        value with "VALUE".                */
  33. /*                                */
  34. /*        Note that "entry" is not copied, it is used     */
  35. /*        as the environment entry. This means that it     */
  36. /*        must not be unallocated or otherwise modifed     */
  37. /*        by the caller, unless it is replaced by a     */
  38. /*        subsequent putenv().                */
  39. /*                                */
  40. /*        If the name is not found in the environment,     */
  41. /*        then a new vector of pointers is allocated,     */
  42. /*        "entry" is put at the end and the global     */
  43. /*        variable "environ" is updated.            */
  44. /*                                */
  45. /*        This function normally returns 0, but -1    */
  46. /*        is returned if it can not allocate enough     */
  47. /*        space using malloc(3), or "entry" does not    */
  48. /*        contain a '='.                    */
  49. /*                                */
  50. /****************************************************************/
  51.  
  52.  
  53. int putenv( entry )
  54.   char *entry;
  55.  
  56.   {
  57.   unsigned long length;
  58.   unsigned long size;
  59.   char     **p;
  60.   char     **new_environ;
  61.  
  62.   /*  Find the length of the "NAME="  */
  63.  
  64.   if ( (length=(unsigned long) index(entry,'=')) == (unsigned long)NULL )
  65.     return( -1 );
  66.  
  67.   length = length - (unsigned long) entry + 1;
  68.  
  69.  
  70.   /*  Scan through the environment looking for "NAME="  */
  71.  
  72.   for ( p=environ; *p != 0 ; p++ )
  73.     if ( strncmp( entry, *p, (int)length ) == 0 )
  74.       {
  75.       *p = entry;
  76.       return( 0 );
  77.       }
  78.  
  79.  
  80.   /*  The name was not found, build a bigger environment  */
  81.  
  82.   size = (unsigned long)(p - environ);
  83.  
  84.   new_environ = (char **) malloc( (unsigned)((size+2)*PSIZE) );
  85.  
  86.   if ( new_environ == NULL )
  87.     return( -1 );
  88.  
  89.   bcopy( (char *) environ, (char *) new_environ, (long)(size*PSIZE) );
  90.  
  91.   new_environ[size]   = entry;
  92.   new_environ[size+1] = NULL;
  93.  
  94.   environ = new_environ;
  95.  
  96.   return(0);
  97.   }
  98.